<template>
  <div>
    <el-table v-loading="listLoading" :data="list">
      <el-table-column align="center" label="id" prop="id" width="50" />
      <el-table-column align="center" label="分类" prop="category" />
      <el-table-column align="center" label="收款渠道" prop="payment_method" />
      <el-table-column align="center" label="金额" prop="amount" />
      <el-table-column align="center" label="收款人" prop="boyi_user.name" />
      <el-table-column align="center" label="备注" prop="note" />
      <el-table-column align="center" label="操作时间" prop="created_at" />
      <!-- <el-table-column align="center" label="是否发放" prop="note">
        <template #default="{ row }">
          <el-tag v-if="row.is_pay" type="success">已发放</el-tag>
          <el-tag v-else type="danger">未发放</el-tag>
        </template>
      </el-table-column> -->
    </el-table>
  </div>
</template>

<script>
  import { getList } from '@/api/boyiFinance'

  export default defineComponent({
    name: 'FinanceList',
    components: {},
    emit: [],
    props: {
      customer: {
        type: Object,
        default: () => {
          return { id: '0' }
        },
      },
    },
    setup(props) {
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')
      const state = reactive({
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
        },
      })

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async (map) => {
        state.queryForm = {
          ...state.queryForm,
          ...map,
        }

        console.log('=============', state.queryForm)
        state.listLoading = true
        const {
          data: { list, total },
        } = await getList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }

      onMounted(() => {
        fetchData({ customer_id: props.customer.id })
      })

      watch(
        () => props.customer,
        (newVal, oldVal) => {
          // 这里是你的处理逻辑
          console.log('customer changed', newVal, oldVal)
          fetchData({ customer_id: newVal.id })
        }
      )

      return {
        ...toRefs(state),
        setSelectRows,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
      }
    },
  })
</script>
